package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import tw.edu.tku.im.maze.EnterRoomRep;
import tw.edu.tku.im.maze.Packet;
import tw.edu.tku.im.maze.Room;
import tw.edu.tku.im.maze.RoomListRep;
import tw.edu.tku.im.maze.RoomListReq;

public class RoomListProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "roomList";
   }
   
   private String getUserName(String userID) {
      try {
         Connection con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
               con.prepareCall("{call usp_getUserNmae(?,?) }");
         cstmt.setString(1, userID);
         cstmt.registerOutParameter(2, Types.NCHAR);
         cstmt.execute();
         return cstmt.getString(2).trim();
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return null;
   }

   @Override
   Packet process(Packet packet) {
      RoomListReq req = (RoomListReq)packet;
      List<Room> roomList = new ArrayList<Room>();
      try {
         Connection con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
               con.prepareCall("{call usp_roomList() }");
         cstmt.execute();
         ResultSet rs = cstmt.getResultSet();
         while(rs.next()) {
            int roomID = rs.getInt("roomID");
            String room = rs.getString("roomName").trim();
//            String homeowner = rs.getString("homeowner").trim();
            String userID = rs.getString("homeowner").trim();
            String homeowner = getUserName(userID);
            CallableStatement temp = 
               con.prepareCall("{ ? = call usp_getRoomUserMap(?,?) }");
            temp.registerOutParameter(1, Types.INTEGER);
            temp.setInt(2, roomID);
            temp.registerOutParameter(3, Types.TINYINT);
            temp.execute();
            int currentUser = temp.getInt(1);
            int mapLv = temp.getInt(3);
            int mode = rs.getInt("mode");
            String pw = rs.getString("roomPassword");
            boolean privateRoom;
            if(pw!=null) { privateRoom=true; }
            else { privateRoom=false; }
            Room roomTemp = new Room(roomID, room, homeowner, currentUser, 
                                     mapLv, mode, privateRoom);
            roomList.add(roomTemp);
         }
         if(roomList.size()!=0) {
            return new RoomListRep(roomList);
         }
      } catch (SQLException e) {
         e.printStackTrace();
      }
      //...... to do something
//      Room r1 = new Room(2, "Room1", "john1022", 4, 3, 0, true);
//      Room r2 = new Room(1, "Room2", "mike0908", 2, 4, 1, false);
//      Room r3 = new Room(3, "Room3", "andy11", 1, 2, 0, false);
//      Room[] rs1 = new Room[]{r1, r2, r3};
//      RoomListRep rep = new RoomListRep(rs1);
//      return rep;
      return new RoomListRep();
   }

}
